package com.yunsc.admin.controller.order;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.order.dto.OrderDeliveryDTO;
import com.yunsc.order.dto.QueryDeliveryDTO;
import com.yunsc.order.dto.QueryOrderDTO;
import com.yunsc.order.sevice.YunscOrderDeliveryService;
import com.yunsc.order.sevice.YunscOrderService;
import com.yunsc.order.vo.OrderDeliveryVO;
import com.yunsc.order.vo.OrderDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 订单信息接口
 * @date: 2024-04-29 21:53
 */
@Api(value = "订单信息接口", tags = "订单信息接口")
@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
    private YunscOrderService orderService;
    @Resource
    private YunscOrderDeliveryService orderDeliveryService;

    @ApiOperation(value = "查询用户订单列表", tags = "查询用户订单列表")
    @GetMapping("/list")
    public Result<?> getOrderList(QueryOrderDTO params) {
        // 根据dataType和page获取用戶订单列表数据
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        PageResult<OrderDetailVO> orderList = orderService.queryOrderList(params, currentStoreId);
        return Result.resultPageList(orderList);
    }

    @ApiOperation(value = "查询商家订单详情", tags = "根据订单id 查询商家订单详情")
    @GetMapping("/detail")
    public Result<?> getStoreOrderDetail(@RequestParam Integer orderId) {
        OrderDetailVO orderDetail = orderService.queryOrderDetailByOrderId(orderId);
        Map<String, Object> detail = new HashMap<>();
        detail.put("detail", orderDetail);
        return Result.ok(detail);
    }
    @ApiOperation(value = "订单发货")
    @PostMapping("/delivery")
    public Result<?> orderDelivery(@RequestBody @Validated(ValidationGroups.Update.class) OrderDeliveryDTO param) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        orderService.orderDelivery(param);
        return Result.ok("发货成功");
    }
    @ApiOperation(value = "订单发货记录")
    @GetMapping("/delivery/list")
    public Result<?> orderDeliveryList(QueryDeliveryDTO param) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
         PageResult<OrderDeliveryVO> orderDeliveryList = orderDeliveryService.orderDeliveryPageList(param, currentStoreId);
        return Result.resultPageList(orderDeliveryList);
    }

    @ApiOperation(value = "商家备注")
    @PostMapping("/event/updateRemark")
    public Result<?> updateRemark(@RequestBody OrderDeliveryDTO param) {
        orderService.updateRemark(param);
        return Result.ok("备注成功");
    }

    @ApiOperation(value = "删除订单 (逻辑删除)")
    @PostMapping("/delete")
    public Result<?> deleteOrder(@RequestBody @Validated(ValidationGroups.Delete.class) OrderDeliveryDTO param) {
        orderService.deleteOrderById(param.getOrderId());
        return Result.ok("删除成功");
    }


}
